import socket
import threading
import os
import select
import base64
import hashlib
import select
import time


##服务端

def get_headers(data):
    """
    将请求头格式化成字典
    :param data:
    :return:
    """
    header_dict = {}
    data = str(data, encoding='utf-8')
 
    header, body = data.split('\r\n\r\n', 1)
    header_list = header.split('\r\n')
    for i in range(0, len(header_list)):
        if i == 0:
            if len(header_list[i].split(' ')) == 3:
                header_dict['method'], header_dict['url'], header_dict['protocol'] = header_list[i].split(' ')
        else:
            k, v = header_list[i].split(':', 1)
            header_dict[k] = v.strip()
    return header_dict
 
 
def send_msg(conn, msg_bytes):
    """
    WebSocket服务端向客户端发送消息
    :param conn: 客户端连接到服务器端的socket对象,即： conn,address = socket.accept()
    :param msg_bytes: 向客户端发送的字节
    :return:
    """
    import struct
 
    token = b"\x81"
    length = len(msg_bytes)
    if length < 126:
        token += struct.pack("B", length)
    elif length <= 0xFFFF:
        token += struct.pack("!BH", 126, length)
    else:
        token += struct.pack("!BQ", 127, length)
 
    msg = token + msg_bytes
    conn.send(msg)
    return True

def dedupe_dict_by_key(d: dict) -> dict:
    new_dict = {}
    for key, value in d.items():
        if key not in new_dict:
            new_dict[key] = value
    return new_dict
 
 
def run():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(('0.0.0.0', 8800))
    sock.listen(5)
    ins=[sock]
    print('server开启...')
    clients=[]
    outs=[]
    nicks={}
    newnicks={}

    
    while True:
        readab,writeab,errab=select.select(ins,outs,ins)
        for s in readab:
            if s is sock:
                conn, address = sock.accept()
                ins.append(conn)
                clients.append(conn)
            else:
                if s not in outs and s in ins:
                    try:
                        data = s.recv(1024)
                        if data:
                            headers = get_headers(data)
                            response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
                                           "Upgrade:websocket\r\n" \
                                           "Connection:Upgrade\r\n" \
                                           "Sec-WebSocket-Accept:%s\r\n" \
                                           "WebSocket-Location:ws://%s%s\r\n\r\n"
                         
                            value = headers['Sec-WebSocket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
                            ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())
                            response_str = response_tpl % (ac.decode('utf-8'), headers['Host'], headers['url'])
                            s.send(bytes(response_str, encoding='utf-8'))
                            outs.append(s)
                        
                            
                            
                    except:
                        print("pass断开")
                        
                else:           
                    try:
                        
                        info = s.recv(8096)
                    except Exception as e:
                        info = None
                    if not info:
                        break
                    payload_len = info[1] & 127
                    
                    if payload_len == 126:
                        extend_payload_len = info[2:4]
                        mask = info[4:8]
                        decoded = info[8:]
                    elif payload_len == 127:
                        extend_payload_len = info[2:10]
                        mask = info[10:14]
                        decoded = info[14:]
                    else:
                        extend_payload_len = None
                        mask = info[2:6]
                        decoded = info[6:]

                    bytes_list = bytearray()
                    for i in range(len(decoded)):
                        chunk = decoded[i] ^ mask[i % 4]
                        bytes_list.append(chunk)
                    t_msg=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())   
                    try:
                        body = str(bytes_list, encoding='utf-8')
                        body=body+" ["+t_msg+"]"
                        for k in clients:
                            send_msg(k,body.encode('utf-8'))
                        if "：" in  body:
                            nicks[s]=body.split("：")[0]
                            newnicks=dedupe_dict_by_key(nicks)
                    except:
                        clients.remove(s)
                        ins.remove(s)
##                        outs.remove(s)
                        if s not in clients:
                            nik=newnicks.pop(s)
                            print("{},离线".format(nik))
                            for k in clients:
                                uleave="【"+nik+"】已离线"+" ["+t_msg+"]"
                                send_msg(k,uleave.encode('utf-8'))

                    
                        
                        

    sock.close()


if __name__=='__main__':
    run()


        

